package com.bianguo.android.pigeondemo.Presenter;

import com.bianguo.android.pigeondemo.entity.MovieEntity;
import com.bianguo.android.pigeondemo.model.MainModel;
import com.bianguo.android.pigeondemo.model.ModelFactory;
import com.bianguo.android.pigeondemo.retrofit.ApiException;
import com.bianguo.android.pigeondemo.retrofit.BaseSubscriber;
import com.bianguo.android.pigeondemo.utils.MLog;
import com.bianguo.android.pigeondemo.view.IMainView;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;

/**
 * Created by Administrator on 2018/6/4.
 */

public class MainPresenter extends BasePresenter<IMainView, MainModel> {

    public MainPresenter(IMainView mView) {
        super(mView);
        mModel = (MainModel) ModelFactory.getInstance().createModel(ModelFactory.MODEL_TYPE_MAIN);
    }

    public Disposable getMovieList(int page) {
        Disposable disposable = mModel.getMoviesList(page, new Consumer<MovieEntity>() {

            @Override
            public void accept(MovieEntity movieEntity) throws Exception {
                MLog.mLog("getMovieList count = " + movieEntity.getCount());
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                MLog.mLog("getMovieList error = " + throwable.getMessage());
            }
        });
        disposables.put("MainPresenter_getMovieList", disposable);
        return disposable;
    }

    public void getMovieList() {
        mModel.getMoviesList(2, new BaseSubscriber<MovieEntity>(disposables, "MainPresenter_getMovieList") {

            @Override
            public void onNext(@NonNull MovieEntity movieEntity) {
                MLog.mLog("next getMovieList count = " + movieEntity.getCount());
            }

            @Override
            public void onError(ApiException apiException) {
                MLog.mLog("getMovieList onError getMovieList error = " + apiException.getMessage());
            }
        });
        MLog.mLog("getMovieList");
    }
}
